package com.mall.rabbitmq.demo3;

import cn.hutool.json.JSONUtil;
import com.mall.rabbitmq.config.CustomConfirmAndReturnCallback;
import com.mall.vo.DemoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author hyt
 * @date 2021/10/26
 */
@RestController
@Slf4j
@RequestMapping("/rebbit")
public class RebbitController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //需要注入刚刚创建的confirm回调函数
    @Autowired
    private CustomConfirmAndReturnCallback callback;

    @RequestMapping("/test")
    public String demo(){
        System.out.println("处理请求中……");
        callback.init();
        for (int i = 0; i < 3 ; i++) {
            /**
             * 发送消息 默认消息为持久化 消息发送是发送到交换机中 根据对应的key值分配到不同的队列中
             * 如果对应的key值不存在与所有声明的队列导致消息发送失败,就会在ReturnCallback进行回调输出
             * 如果交换机不存在导致消息发送失败则会在ConfirmCallback进行回调输出
             * exchange:交换机
             * routingKey:消息发送key 在配置中将队列绑定到交换机中时 声明了key的值
             * Object ：发送的消息
             */
            rabbitTemplate.convertAndSend("item_topic_exchange", "item.1", "demo"+i);
            log.error("Something else is wrong here");
        }
        return "ok";
    }

    @RequestMapping("/test1")
    public String dem1o(){
        DemoVO vo = new DemoVO();
        vo.setDestination("1");
        rabbitTemplate.convertAndSend("item_topic_exchange", "item.new", JSONUtil.toJsonStr(vo));
        rabbitTemplate.convertAndSend("item_topic_exchange", "item.1", "demo");
        return "ok";
    }

}
